package com.bac.bacplatform;


import java.util.HashMap;
import java.util.Map;

import com.android.volley.Response.Listener;
import com.bac.utils.Config;
import com.bac.utils.Util;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class EditPayPwdActivity extends BaseActivity implements OnClickListener{
	private String cur_paypwd;
	private String new_paypwd;
	private String confirm_paypwd;
	
	private EditText cur_paypwdEditText;
	private EditText new_paypwdEditText;
	private EditText confirm_paypwdEditText;
	
	private ImageButton clear_cur_paypwdButton;
	private ImageButton clear_new_paypwdButton;
	private ImageButton clear_confirm_paypwdButton;
	
	private Button mConfirmButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.paypassword_edit);
		findViewById(R.id.title_left).setOnClickListener(this);

	}

	@Override
	public void init() {
		cur_paypwdEditText = (EditText) findViewById(R.id.cur_paypwd_text);
		new_paypwdEditText = (EditText) findViewById(R.id.new_paypwd_text);
		confirm_paypwdEditText = (EditText) findViewById(R.id.confirm_paypwd_text);
		
		clear_cur_paypwdButton = (ImageButton) findViewById(R.id.btn_clear_cur_paypwd);
		clear_cur_paypwdButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				cur_paypwdEditText.setText("");
			}
		});
		
		clear_new_paypwdButton = (ImageButton) findViewById(R.id.btn_clear_new_paypwd);
		clear_new_paypwdButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new_paypwdEditText.setText("");
			}
		});
		
		clear_confirm_paypwdButton = (ImageButton) findViewById(R.id.btn_clear_confirm_paypwd);
		clear_confirm_paypwdButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				confirm_paypwdEditText.setText("");
			}
		});
		
		cur_paypwdEditText.addTextChangedListener(textWatcher);
		cur_paypwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (cur_paypwdEditText.getText().toString() != null
							&& !("").equals(cur_paypwdEditText.getText().toString())) {
						clear_cur_paypwdButton.setVisibility(View.VISIBLE);
						clear_new_paypwdButton.setVisibility(View.INVISIBLE);
						clear_confirm_paypwdButton.setVisibility(View.INVISIBLE);
					}
				}else {
					clear_cur_paypwdButton.setVisibility(View.INVISIBLE);
				}
			}
		});
		new_paypwdEditText.addTextChangedListener(textWatcher);
		new_paypwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (new_paypwdEditText.getText().toString() != null
							&& !("").equals(new_paypwdEditText.getText().toString())) {
						clear_new_paypwdButton.setVisibility(View.VISIBLE);
						clear_cur_paypwdButton.setVisibility(View.INVISIBLE);
						clear_confirm_paypwdButton.setVisibility(View.INVISIBLE);
					}
				}else {
					clear_new_paypwdButton.setVisibility(View.INVISIBLE);
				}
			}
		});
		confirm_paypwdEditText.addTextChangedListener(textWatcher);
		confirm_paypwdEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					if (confirm_paypwdEditText.getText().toString() != null
							&& !("").equals(confirm_paypwdEditText.getText().toString())) {
						clear_confirm_paypwdButton.setVisibility(View.VISIBLE);
						clear_cur_paypwdButton.setVisibility(View.INVISIBLE);
						clear_new_paypwdButton.setVisibility(View.INVISIBLE);
					}
				}else {
					clear_confirm_paypwdButton.setVisibility(View.INVISIBLE);
				}
			}
		});
		
		mConfirmButton = (Button) findViewById(R.id.paypassword_edit_btn);
		mConfirmButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ChangePayPwd();
			}
		});

	}
	
	TextWatcher textWatcher = new TextWatcher() {
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void afterTextChanged(Editable s) {
			if (cur_paypwdEditText.hasFocus() 
					&& cur_paypwdEditText.getText().toString() != null 
					&& !("").equals(cur_paypwdEditText.getText().toString())) {
				clear_cur_paypwdButton.setVisibility(View.VISIBLE);
			}else {
				clear_cur_paypwdButton.setVisibility(View.INVISIBLE);
			}
			
			if (new_paypwdEditText.hasFocus() 
					&& new_paypwdEditText.getText().toString() != null 
					&& !("").equals(new_paypwdEditText.getText().toString())) {
				clear_new_paypwdButton.setVisibility(View.VISIBLE);
			}else {
				clear_new_paypwdButton.setVisibility(View.INVISIBLE);
			}
			
			if (confirm_paypwdEditText.hasFocus() 
					&& confirm_paypwdEditText.getText().toString() != null 
					&& !("").equals(confirm_paypwdEditText.getText().toString())) {
				clear_confirm_paypwdButton.setVisibility(View.VISIBLE);
			}else {
				clear_confirm_paypwdButton.setVisibility(View.INVISIBLE);
			}
			
		}
	};
	
	public void ChangePayPwd(){
		/**
		 * 成功返回结果
		 */
		Listener<String> sl = new Listener<String>() {
			@Override
			public void onResponse(String response) {
				dismissLoadingProgressNow();
				Log.i("TAG----success","修改支付密码成功");
				showToast("修改支付密码成功");
				finishActivityOut();
			}
		};
		
		cur_paypwd = cur_paypwdEditText.getText().toString().trim();
		new_paypwd = new_paypwdEditText.getText().toString().trim();
		confirm_paypwd = confirm_paypwdEditText.getText().toString().trim();
		//数据验证
				if (cur_paypwd.length() <= 0) {
					showToast(R.string.password_none);
					return;
				}
				if (new_paypwd.length() <= 0 || confirm_paypwd.length() <= 0) {
					showToast(R.string.input_newpassword);
					return;
				}
				if (!(new_paypwd.equals(confirm_paypwd))) {
					showToast(R.string.different_pwd);
					return;
				}
				if (new_paypwd.equals(cur_paypwd)) {
					showToast(R.string.same_pwd); 
					return;
				}
		showLoadingProgress();
		String path = Config.URL_CHANGE_PAY_PASSWORD;
		Map<String, String> map = new HashMap<String, String>();
		map.put("oldPassword",cur_paypwd);
		map.put("password", new_paypwd);
		Util.httpStringPost(path, sl, this, map);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left:
			finishActivityOut();
			break;
		default:
			break;
		}
	}

}
